﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint.WorkflowActions;
using Microsoft.SharePoint;
using Microsoft.Office.RecordsManagement.RecordsRepository;

namespace QNH.ActivityLibrary
{
    public partial class UnRecordDocumentSet : SequenceActivity
    {
        #region Static dependency properties

        public static DependencyProperty __ContextProperty = DependencyProperty.Register(
            "__Context", typeof(WorkflowContext), typeof(UnRecordDocumentSet));

        #endregion

        #region Properties

        [ValidationOption(ValidationOption.Optional)]
        public WorkflowContext __Context
        {
            get
            {
                return (WorkflowContext)base.GetValue(__ContextProperty);
            }
            set
            {
                base.SetValue(__ContextProperty, value);
            }
        }

        #endregion

        public UnRecordDocumentSet()
        {
            InitializeComponent();
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            try
            {
                using (SPWeb web = this.__Context.Web)
                {
                    SPList list = web.Lists[new Guid(__Context.ListId)];
                    SPListItem listItem = list.Items.GetItemById(__Context.ItemId);
                    if (listItem.Folder != null)
                    {
                        foreach (SPFile spFile in listItem.Folder.Files)
                        {
                            try
                            {
                                Records.UndeclareItemAsRecord(spFile.Item);
                            }
                            catch (Exception e)
                            {
                                HelperMethods.ErrorHandling(e);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                HelperMethods.ErrorHandling(e);
            }

            return ActivityExecutionStatus.Closed;
        }
    }


}
